package com.blakequ.androidblemanager.utils;

import android.content.Intent;
import android.util.Log;

import com.blakequ.androidblemanager.containers.BluetoothLeDeviceStore;
import com.blakequ.androidblemanager.event.RequestBleModel;
import com.orhanobut.logger.Logger;
import com.pgyer.pgyersdk.PgyerSDKManager;
import com.tencent.mmkv.MMKV;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class BLEUtil {
    public static BLEUtil getInstance() {
        if (instance == null) {
            instance = new BLEUtil();
        }
        return instance;
    }

    static BLEUtil instance;

    private BluetoothUtils mBluetoothUtils;

    public BluetoothUtils getmBluetoothUtils() {
        return mBluetoothUtils;
    }

    public void setmBluetoothUtils(BluetoothUtils mBluetoothUtils) {
        this.mBluetoothUtils = mBluetoothUtils;
    }

    public BluetoothLeDeviceStore getmDeviceStore() {
        return mDeviceStore;
    }

    public void setmDeviceStore(BluetoothLeDeviceStore mDeviceStore) {
        this.mDeviceStore = mDeviceStore;
    }

    private BluetoothLeDeviceStore mDeviceStore;

    private long sendTimes = 0;

    public long getSendTimes() {
        return sendTimes;
    }

    public void setSendTimes(long sendTimes) {
        this.sendTimes = sendTimes;
    }

    public long getSuccessTimes() {
        return successTimes;
    }

    public void setSuccessTimes(long successTimes) {
        this.successTimes = successTimes;
    }

    public long getFailedTimes() {
        return failedTimes;
    }

    public void setFailedTimes(long failedTimes) {
        this.failedTimes = failedTimes;
    }

    private long successTimes = 0;
    private long failedTimes = 0;
    public void requestSubmit(String data) {
//        data = "012600080229B1A7";
        RequestBleModel requestBleModel = new RequestBleModel();
        requestBleModel.setData(data);
        requestBleModel.setDevaddr(MMKV.defaultMMKV().getString("id","1"));
        String dataJson = JsonUtil.toString(requestBleModel);
//        dataJson = "{\"tick\":\"1646101349\",\"data\":\"012600080229B1A7\",\"msgType\":1,\"devaddr\":\"2\"}";
        Log.i("requestSubmit", "发送json:" + dataJson);
        String jsonBody = "xiachao";
        try {
            jsonBody = URLEncoder.encode(dataJson,"UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
//        http://124.222.17.202:9090/beacon/uploadLora?data={"tick":
//        OKHttpUtil.getInstance().post("http://192.168.118.28:8080/api/uploadLora?data="+jsonBody, dataJson, new Callback() {
//        OKHttpUtil.getInstance().post("http://192.168.118.28:8080/api/uploadLora?data="+jsonBody, dataJson, new Callback() {
        OKHttpUtil.getInstance().post("http://124.222.17.202:9090/beacon/uploadLora?data="+jsonBody, dataJson, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                sendTimes++;
                failedTimes++;
                printSendTimesLog();
                ToastX.show(e.toString());
                PgyerSDKManager.reportException(e);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                try {
                    successTimes++;
                    sendTimes++;
                    printSendTimesLog();
                    if (response.isSuccessful()) {
                        String body = response.body().string();
                        Log.i("BOX-Debug", "loginJson:" + body);

                        Log.i("message", body);
                    } else {
                        String body = response.body().string();
                        ToastX.show(response.message() + body);
                        try {
                            PgyerSDKManager.reportException(new Exception(response.message() + body));
                        } catch (Exception e) {
                            if (e != null) {
                                Log.e("",""+e.toString());
                            }
                        }
                    }
                } catch (Exception e) {
                    if (e != null) {
                        Log.e("", "" + e.toString());
                        ToastX.show(e.toString());
                    }
                }
            }
        });
    }

    private void printSendTimesLog() {
        Logger.i("扫描结果，第"+sendTimes+"次发送结果,成功:"+successTimes+"次,失败:"+failedTimes);
    }

    /**
     * 超过最大时间
     * @param timestamp
     * @return
     */
    public static boolean isTooLong(long timestamp) {
        return (System.currentTimeMillis() - timestamp) > 13000;
    }

}
